﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BahdlClassLib;
using BusinessLayer;

namespace MasterForms
{
    public partial class FinancialYear : CustomFormTemplate
    {
        public FinancialYear()
        {
            InitializeComponent();
        }

        void FinancialYear_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Escape)
            {
                if (this.Tag.ToString() == "view")
                {
                    FinancialYear_ToolBarButtonClick("exit");
                }
                else
                {
                    FinancialYear_ToolBarButtonClick("revert");
                }
            }
        }
        void FinancialYear_EnableControls(bool Enable)
        {
            fraDtl.Enabled = Enable;
        }
        private void FinancialYear_ToolBarButtonClick(string Button)
        {
            if (CommonFunction.InList(Button, "add", "edit", "delete") == true)
            {
                if ((Button == "edit" || Button == "delete"))
                {
                    //if (dtFrom.Text == dtTo.Text )
                    //{
                    //    return;
                    //}
                }
            }
            switch (Button)
            {
                case "exit":
                    if (UIFunction.SureToExit("") == true)
                    {
                        this.Close();
                    }
                    break;
                case "revert":
                    this.Tag = "view";
                    FinancialYear_EnableControls(false);
                    this.ToolBarEnterEditMode = false;
                    break;
               
                case "find":
                    DataSet dsLocal = new DataSet();
                    Popup popup = new Popup();
                    try
                    {
                        ClsFinancialYear ClsFinancialYear = new ClsFinancialYear();
                        dsLocal.Tables.Add(ClsFinancialYear.GetAllFinancialYear());
                        dsLocal.Tables[0].TableName = "popup";
                        popup.Width = 40;
                        popup.ShowPopup(null, "FinancialYearID", ref dsLocal, "popup", "StartDay,EndDay,Active", "", "Manage Financial Year", "Start Period,End period,Active", "12,12,8");
                        if (!string.IsNullOrEmpty(CommonFunction.NullToString(popup.ReturnValue())))
                        {
                            uiGetData(Convert.ToInt32(popup.Field("FinancialYearID")));
                        }
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    finally
                    {
                        dsLocal = null;
                        popup = null;
                    }
                    break;
                case "add":
                    if (uiGetData(0))
                    {
                        this.ToolBarEnterEditMode = true;
                        this.Tag = "add";
                        FinancialYear_EnableControls(true);
                        dtFrom.Select();
                        dtTo.Select();
                        chkActive.Select();                        
                    }
                    break;
                case "edit":
                    this.ToolBarEnterEditMode = true;
                    this.Tag = "edit";
                    FinancialYear_EnableControls(true);
                    dtFrom.Select();
                    dtTo.Select();
                    chkActive.Select();
                    break;
                case "save":
                    if (uiPutData() == true & this.Tag.ToString() == "edit")
                    {
                        this.Tag = "view";
                        this.ToolBarEnterEditMode = false;
                        FinancialYear_EnableControls(false);
                    }
                    break;
                case "delete":
                    uiDeleteData();
                    break;
            }


        }


        private void uiDeleteData()
        {
            try
            {
                ClsFinancialYear ClsFinancialYear = new ClsFinancialYear();
                ClsFinancialYear.FinancialYearID = Convert.ToInt32(fraDtl.Tag);
                ClsFinancialYear.DeleteFinancialYear();
                uiGetData(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
        }
        private bool uiPutData()
        {
            bool functionReturnValue = false;
            try
            {
                  ClsFinancialYear ClsFinancialYear = new ClsFinancialYear();
                  ClsFinancialYear.FinancialYearID =  Convert.ToInt32(fraDtl.Tag);
                  ClsFinancialYear.StartDay = Convert.ToDateTime(dtFrom.Text);
                  ClsFinancialYear.EndDay = Convert.ToDateTime(dtTo.Text);
                  ClsFinancialYear.chkActive = Convert.ToBoolean(chkActive.Checked);
       
                if (ClsFinancialYear.SaveFinancialYear(this.Tag.ToString()))
                    functionReturnValue = true;

                uiGetData(0);

                if (this.Tag.ToString() == "add")
                    dtFrom.Select();
                    dtTo.Select();
                    chkActive.Select();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            return functionReturnValue;
        }
        private bool uiGetData(int KeyField)
        {
            bool functionReturnValue = false;
            try
            {
                ClsFinancialYear ClsFinancialYear = new ClsFinancialYear();
                ClsFinancialYear.GetFinancialYear(KeyField);

                fraDtl.Tag = ClsFinancialYear.FinancialYearID;
                fraDtl.Tag = KeyField;

                dtFrom.Text = ClsFinancialYear.StartDay.ToString();
                dtTo.Text = ClsFinancialYear.EndDay.ToString();
                chkActive.Checked  = Convert.ToBoolean(ClsFinancialYear.chkActive);              

                functionReturnValue = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            return functionReturnValue;
        }

        private void LeaveTypes_Load(object sender, EventArgs e)
        {
            
            this.Left = 0;
            this.Top = 0;
            this.Tag = "view";
          
            base.ToolBarButtonClick += new ToolBarButtonClickEventHandler(FinancialYear_ToolBarButtonClick);
            base.EnableControls += new EnableControlsEventHandler(FinancialYear_EnableControls);
            base.KeyDown += new KeyEventHandler(FinancialYear_KeyDown);
        }
       
    }
}
